/*
 * DocumentHandler.java
 *
 * Created on 2 September 2006, 1:41
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package fasea.index.filehandler;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FilenameUtils;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;

/**
 *
 * @author gvasil
 */
public abstract class FileHandler{
    
    private boolean STORE_THUMBNAIL;
    private boolean COMPRESSED;

    public void setStoreThumbnail(boolean b) {
        this.STORE_THUMBNAIL = b;
    }

    public boolean getStoreThumbnail() {
        return this.STORE_THUMBNAIL;
    }

    public void setCompressed(boolean b) {
        this.COMPRESSED = b;
    }

    public boolean isCompressed() {
        return this.COMPRESSED;
    }
    
    public String getFileExtension(File file){
        return FilenameUtils.getExtension(file.getName());
    }
    
    public String getOnlyFileName(File file){
        return FilenameUtils.removeExtension(file.getName());
    }
    
    public Document getDocument(File file) throws FileHandlerException{
        return getTypedDocument(file);
    }

    protected Document createDefaultLuceneDocument(File file) throws FileHandlerException{
    try {
            Document document = new Document();
            document.add(new Field("path", file.getCanonicalPath(),
                    Field.Store.YES, Field.Index.NOT_ANALYZED));
            document.add(new Field("size", String.valueOf(file.length()),
                    Field.Store.YES, Field.Index.NOT_ANALYZED));
            document.add(new Field("inpath",
                    file.getCanonicalPath().replace(File.separatorChar, ' '),
                    Field.Store.YES,
                    Field.Index.ANALYZED));
            document.add(new Field("lastModified", String.valueOf(file.lastModified()),
                Field.Store.YES, Field.Index.NO));
            document.add(new Field("filename", getOnlyFileName(file), Field.Store.YES,
                Field.Index.ANALYZED));
            document.add(new Field("fileExtension", getFileExtension(file), Field.Store.YES,
                Field.Index.NOT_ANALYZED));
            document.add(new Field("documentType", DocumentType.parse(file).name(), Field.Store.YES,
                Field.Index.NOT_ANALYZED));
            return document;
        } catch (IOException e) {
            throw new FileHandlerException(e.getMessage());
        }
    }
    
    protected abstract Document getTypedDocument(File file) throws FileHandlerException;
}



